package com.example.myapplication2;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MmsActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView photo10;
    private TextView mmsContent10;
    private TextView titleEdit10;
    private TextView numberEdit10;
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;
    private Uri data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mms);
        findViewById(R.id.saveMMS).setOnClickListener(this);
        findViewById(R.id.photo10).setOnClickListener(this);
        photo10 = findViewById(R.id.photo10);
        mmsContent10 = findViewById(R.id.mmsContent10);
        titleEdit10 = findViewById(R.id.titleEdit10);
        numberEdit10 = findViewById(R.id.numberEdit10);
        //回调方法
        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode()==RESULT_OK){
                    Intent intent = result.getData();
                    //获得Uri对象
                    data = intent.getData();
                    if (data !=null){
                        //输出显示
                        photo10.setImageURI(data);
                        System.out.println("输出:"+ data.toString());
                    }
                }
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.saveMMS:
                sendMMS();
                break;
            case R.id.photo10:
                //点击空白照片,跳转到相册,选中图片后返回
                Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                //设置获得内容类型
                intent.setType("image/*");
                intentActivityResultLauncher.launch(intent);
                break;
        }
    }

    private void sendMMS() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        //设置任务栈类型
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //目标号码
        intent.putExtra("address",numberEdit10.getText().toString());
        //标题
        intent.putExtra("subject",titleEdit10.getText().toString());
        //内容
        intent.putExtra("sms_body",mmsContent10.getText().toString());
        //附件, content://com.android.providers.media.documents/document/image%3A193281
        intent.putExtra(Intent.EXTRA_STREAM,data);
        //附件类型
        intent.setType("image/*");
        //Intent的接受者允许读取intent对象携带的URI数据
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }
}